import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;

import java.net.URL;

import java.io.BufferedReader;  
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;  
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;  

public class email {

    public static void main(String args[]){    

        URL url;

        int responsecode;

        HttpURLConnection urlConnection;

        BufferedReader reader;

        String bianma;
           
        try{
            url=new URL("http://www.sojson.com/open/api/weather/json.shtml?city=常州");

            urlConnection = (HttpURLConnection)url.openConnection();

            responsecode=urlConnection.getResponseCode();

          
            FileWriter writer = new FileWriter("fore.txt");
            
            
            
            if(responsecode==200){
                reader=new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
                bianma=reader.readLine();
              
               // byte[] jiema= line.getBytes("GBK") ; //解码  
             //   String   bianma = new String(jiema,"UTF-8");//编码 如果上面的解码不对 可能出现问题
  
                String forecast,high,low,israin,high2,low2,israin2;
                forecast = bianma.substring(bianma.indexOf("forecast"),bianma.indexOf("]"));
               
                high = forecast.substring(forecast.indexOf("高温")+2,forecast.indexOf("高温")+5);
                low = forecast.substring(forecast.indexOf("低温")+2,forecast.indexOf("低温")+5);
                israin = forecast.substring(forecast.indexOf("type")+6,forecast.indexOf("type")+12);
               // System.out.println(israin);
                israin = israin.substring(israin.indexOf(":")+2,israin.indexOf(",")-1);
              //  System.out.println(israin);
                
                forecast = forecast.substring(forecast.indexOf(",{"));
                high2 = forecast.substring(forecast.indexOf("高温")+2,forecast.indexOf("高温")+5);
                low2 = forecast.substring(forecast.indexOf("低温")+2,forecast.indexOf("低温")+5);
                israin2 = forecast.substring(forecast.indexOf("type")+6,forecast.indexOf("type")+12);
                //System.out.println(israin2);
                israin2 = israin2.substring(israin2.indexOf(":")+2,israin2.indexOf(",")-1);
               // System.out.println(israin2);
               // System.out.println("今天气温： "+low+"℃~"+high+"℃ "+israin+"\n"+"明天气温： "+low2+"℃~"+high2+"℃ "+israin2);
                
                
                
               
                String message = "今天气温： "+low+"℃~"+high+"℃ "+israin+"\r\n"+"明天气温： "+low2+"℃~"+high2+"℃ "+israin2;
               // String command = "echo "+message+" | mail  -v -s \"天气预报\" 986472954@qq.com";
                String command =  "mail -s 天气预报 986472954@qq.com  < fore.txt;mail -s 天气预报 609294510@qq.com  < fore.txt";
                
                //System.out.println(bianma);
                System.out.println(command);
               // System.out.println(message);
                writer.write(message);
                writer.close();
              ///*
                Process p = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",command},null,null);
   
                InputStream in = p.getInputStream();  
                BufferedReader read = new BufferedReader(new InputStreamReader(in));  
                String line1 = null;  
                p.waitFor();
                while((line1 = read.readLine())!=null){  
                    System.out.println(line1);  
                }  
                System.out.println("程序运行结束");
              //*/
            }

            else{

                System.out.println("失败"+responsecode);

            }

        }

        catch(Exception e){

            System.out.println("失败2"+e);

        }

    }

}